package me.heyclock.hl.recordwriter.app;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;

import me.heyclock.hl.recordwriter.tools.AppUtils;
import me.heyclock.hl.recordwriter.tools.HC_Screen;

/**
 * 自定义Application，用于初始化一些第三方服务
 * Created by hl on 2018/3/12.
 */

public class MyApplication extends Application implements Thread.UncaughtExceptionHandler{
    private static Context mContext;

    @Override
    public void onCreate() {
        super.onCreate();
        this.mContext = getApplicationContext();
        ///< 科大讯飞初始化
        SpeechUtility.createUtility(this, SpeechConstant.APPID +"=5b177202");
        ///< 屏幕工具初始化
        HC_Screen.init(this);
        ///< TODO 用户目录创建
        AppUtils.initAppPath("huanglei");
        ///< 设置Thread Exception Handler
        //Thread.setDefaultUncaughtExceptionHandler(this);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }

    public static Context getInstance(){
        return mContext;
    }

    /**
     * 异常重启 - HomeActivity里面做了fragment处理
     * @param thread
     * @param throwable
     */
    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {
        Log.e("home", "uncaughtException()" + throwable.getMessage());
        System.exit(0);
        Intent intent = new Intent(this, SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}